<?php
class ControllerBase extends VTController {
	/**	 * UserSessionDataService object	 * 	 * @var UserSessionDataService object	 */
	public $userSessionDataService = null;
	
	/**	 * UsersDataService object	 * 	 * @var UsersDataService object	 */
	public $usersDataService = null;
	
	/**	 * Constructor of Controller Base	 */
	public function __construct() {
		parent::__construct ();
		
		// Init Authentication class		$authentication = new Authentication ();
		
		// Only administrator can be right		if (!($authentication->isAdministrator () == true || $authentication->getUserType() == ROLE_ACCOUNTANT || $authentication->getUserType() == ROLE_DEALMANAGER)) {

			// Go on to login page			$url = Zend_Registry::get ( 'config' )->APPLICATION_URL . '/Admin/Login/index';
			CommonUtils::redirect ( $url );
			return - 1;
		}
		
		// Get user online		$this->userSessionDataService = new UserSessionDataService ();
		$this->smarty->assign ( 'sessionUserOnline', $this->userSessionDataService->getUserOnline ( 'createdTime DESC', 0, 10 ) );
		
		// Geting all latest purchase products		$packageOrderDataService = new PackageOrderDataService ();
		
		$arrElement = array ();
		$now = date ( "Y-m-d H:i:s" );
		$arrElement ['status'] = PackageOrderEntity::COMPLETED;
		$arrElement [] = " expiredDate >= '{$now}'";
		$arrLatestPackageEntity = $packageOrderDataService->getItems ( $arrElement, 'createdDate DESC', 0, 30 );
		$this->smarty->assign ( 'arrLatestPackageEntity', $arrLatestPackageEntity );
		$this->smarty->assign ( 'countActivePackage', count ( $arrLatestPackageEntity ) );
		
		$arrElement = array ();
		$now = date ( "Y-m-d H:i:s" );
		$arrElement ['status'] = PackageOrderEntity::COMPLETED;
		$arrElement [] = " expiredDate < '{$now}'";
		$arrLatestPackageEntity = $packageOrderDataService->getItems ( $arrElement, 'createdDate DESC', 0, 30 );
		$this->smarty->assign ( 'arrExpiredPackageEntity', $arrLatestPackageEntity );
		$this->smarty->assign ( 'countExpiredPackage', count ( $arrLatestPackageEntity ) );
		$this->smarty->assign ( 'statusExpired', "Expired" );
		
		$this->smarty->assign ( 'isSuperAdmin', $authentication->isSuperAdministrator () );
		$this->smarty->assign ( 'sUserType', $authentication->getUserType());
	}
	
	/**	 * Create new action key	 *	 * @return void	 */
	public function createActionKey() {
		// Create new token key		$_SESSION ['tokenKey'] = sha1 ( CommonUtils::randomNumber ( 10 ) );
		
		$this->smarty->assign ( 'actionKey', $_SESSION ['tokenKey'] );
	}
	
	/**	 * Validate Action Key	 *	 * @return bool	 */
	public function validateActionKey() {
		// Get action key		$actionKey = $this->getPost ( 'actionKey', '' );
		if (! isset ( $_SESSION ['tokenKey'] ) || $actionKey == $_SESSION ['tokenKey']) {
			//Key has been validated			return true;
		}
		
		return false;
	}
	
	/**	 * Set page name	 *	 * @param string $pageName: name of page	 */
	public function setPageName($pageName) {
		$this->smarty->assign ( 'pageName', $pageName );
	}
}